﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppManagement
{
    #region Using
    using DevComponents.DotNetBar;
    using AppManagement.UsersManager;
    using AppManagement.Helpers;
    using AppManagement.Repositories;
    using AppManagement.UserManagers;
    using AppManagement.MainManagers;
    using AppManagement.Models; 
    #endregion

    public partial class MainForm : Office2007Form 
    {
        public MainForm()
        {
            InitializeComponent();
            InitControls();
        }

        private void InitSoLuongTon()
        {
            // Cap nhat so luong ton cho qui truoc do, trong truong hop lan dau tien su dung phan mem
            if (TONKHODAUKYRepository.SelectAll().Count == 0)
            {
                var lstThietBi = THIETBIRepository.SelectAll();
                var qui = CommonHelpers.CalculateQui(DateTime.Now.Month);
                var nam = DateTime.Now.Year;

                foreach (var item in lstThietBi)
                {
                    var nam2 = qui == 1 ? nam - 1 : nam;
                    var qui2 = nam2 == nam ? qui - 1 : 4;
                    var tondauky = new TONKHODAUKY
                    {
                        IDThietBi = item.THIET_BI_ID,
                        Qui = qui2,
                        Nam = nam2,
                        SoLuongTon = THIETBIRepository.SelectByID(item.THIET_BI_ID).SL_TON.Value
                    };
                    TONKHODAUKYRepository.Insert(tondauky);
                }
            }
        }

        private void menQuanLyChucNangNhomNguoiDung_Click(object sender, EventArgs e)
        {
            FrmQuanLyCHUCNANG_LOAINGUOIDUNG frm = new FrmQuanLyCHUCNANG_LOAINGUOIDUNG();
            frm.ShowDialog();
        }

        private void menQuanLyLoaiNguoiDung_Click(object sender, EventArgs e)
        {
            FrmQuanLyLOAINGUOIDUNG frm = new FrmQuanLyLOAINGUOIDUNG();
            frm.ShowDialog();
        }

        private void menQuanLyNguoiDung_Click(object sender, EventArgs e)
        {
            FrmQuanLyNGUOIDUNG frm = new FrmQuanLyNGUOIDUNG();
            frm.ShowDialog();
        }

        private void menDangXuat_Click(object sender, EventArgs e)
        {
            menDangNhap.Enabled = true;
            GlobalVariables.g_CurrentUser = null;

            FrmDangNhap frm = new FrmDangNhap();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        public void ValidateLogin(object sender, EventArgs e)
        {
            bool success = true;
            string infoText = "";
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            string username = comp[0].Trim();
            string password = comp[1].Trim();
            string PasswordEnc = Encryption.Encrypt(password);

            GlobalVariables.g_CurrentUser = NGUOIDUNGRepository.SelectByUserName(username);

            if (GlobalVariables.g_CurrentUser == null)
            {
                success = false;
                infoText = "Người dùng này chưa có trong hệ thống";
            }
            else
            {
                GlobalVariables.g_CurrentUser = NGUOIDUNGRepository.SelectByUserNameAndPassword(username, PasswordEnc);
                if (GlobalVariables.g_CurrentUser == null)
                {
                    success = false;
                    infoText = "Bạn đã nhập sai mật khẩu";
                }
                else
                {
                    infoText = "Chào bạn  " + GlobalVariables.g_CurrentUser.TEN_DANG_NHAP + "  vào hệ thống !";
                }
            }

            if (!success)
            {
                MessageBoxEx.Show(infoText, "Log In", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                // Re-login
                FrmDangNhap frm = new FrmDangNhap();
                frm.Handler += ValidateLogin;
                frm.ShowDialog();
            }
            else
            {
                menDangNhap.Enabled = false;
                InitControls();

                // Get current user
                GlobalVariables.g_CurrentUser = NGUOIDUNGRepository.SelectByUserName(username);
                // Mapping name of controls with specifed enums
                var dictFucns = GlobalUserManagers.InitFuncsMapping();
                // Store list of functionalities that user allow using
                var funcsAllowUsing = GlobalUserManagers.InitFuncsAllowUsing();

                // Disable some functions that user not allow using
                foreach (var item in dictFucns)
                {
                    foreach (var item2 in funcsAllowUsing)
                    {
                        if (item.Value == item2)
                        {
                            GlobalUserManagers.EnableFuncAllowUsing(pnlMain, item.Key);
                            break;
                        }
                    }
                }

                InitSoLuongTon();
                MessageBoxEx.Show(infoText);
            }
        }

        public void InitControls()
        {
            // Nhan vien
            menQuanLyChucNangNhomNguoiDung.Enabled = false;
            menQuanLyLoaiNguoiDung.Enabled = false;
            menQuanLyNguoiDung.Enabled = false;

            menNhaCungCap.Enabled = false;
            menChiNhanh.Enabled = false;
            menLoaiThietBi.Enabled = false;
            menThietBi.Enabled = false;

            ttbtnLapPhieuYeuCau.Enabled = false;
            ttbtnLapPhieuDatHang.Enabled = false;
            ttbtnQuanLyPhieuYeuCau.Enabled = false;
            ttbtnQuanLyPhieuDatHang.Enabled = false;
            ttbtnQuanLyPhieuNhapKho.Enabled = false;
            ttbtnQuanLyPhieuXuatKho.Enabled = false;
            ttbtnTonKho.Enabled = false;
        }

        private void menDangNhap_Click(object sender, EventArgs e)
        {
            FrmDangNhap frm = new FrmDangNhap();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string ErrorText = "";

            if (!System.IO.File.Exists(GlobalVariables.g_strPathConfig))
            {
                FrmKetNoiCSDL frm = new FrmKetNoiCSDL();
                frm.ShowDialog();
            }
            else
            {
                XMLProvider _provider = new XMLProvider();
                // Load config file to get infos that used to connect to DB
                _provider.GetConnectionInfo(ref ErrorText);
                if (ErrorText != "")
                {
                    MessageBoxEx.Show(ErrorText, "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    // Re-create connection infos
                    FrmKetNoiCSDL frm = new FrmKetNoiCSDL();
                    frm.ShowDialog();
                }
                else
                {
                    // Get connection string successfull, then open connection
                    var lstNguoiDung = NGUOIDUNGRepository.SelectAll();
                    if (lstNguoiDung.Count > 0)
                    {
                        FrmDangNhap frm = new FrmDangNhap();
                        frm.Handler += ValidateLogin;
                        frm.ShowDialog();
                    }
                    else
                    {
                        // Chua co nguoi dung nao trong he thong, nguoi dung dau tien se giu vai tro la admin
                        menDangNhap.Enabled = false;
                        menDangXuat.Enabled = false;
                        menDoiMatKhau.Enabled = false;
                        menQuanLyChucNangNhomNguoiDung.Enabled = true;
                        menQuanLyLoaiNguoiDung.Enabled = true;
                        menQuanLyNguoiDung.Enabled = true;
                    }
                }
            }
        }

        private void menDoiMatKhau_Click(object sender, EventArgs e)
        {
            FrmDoiMatKhau frm = new FrmDoiMatKhau();
            frm.ShowDialog();
        }

        private void menThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menNhaCungCap_Click(object sender, EventArgs e)
        {
            FrmQuanLyNHACUNGCAP frm = new FrmQuanLyNHACUNGCAP();
            frm.ShowDialog();
        }

        private void menChiNhanh_Click(object sender, EventArgs e)
        {
            FrmQuanLyCHINHANH frm = new FrmQuanLyCHINHANH();
            frm.ShowDialog();
        }

        private void menLoaiThietBi_Click(object sender, EventArgs e)
        {
            FrmQuanLyLOAITHIETBI frm = new FrmQuanLyLOAITHIETBI();
            frm.ShowDialog();
        }

        private void menThietBi_Click(object sender, EventArgs e)
        {
            FrmQuanLyTHIETBI frm = new FrmQuanLyTHIETBI();
            frm.ShowDialog();
        }

        private void ttbtnQuanLyCongTrinh_Click(object sender, EventArgs e)
        {
            FrmLapPHIEUYEUCAU frm = new FrmLapPHIEUYEUCAU();
            frm.ShowDialog();
        }

        private void menPhieuYeuCau_Click(object sender, EventArgs e)
        {
            FrmQuanLyPHIEUYEUCAU frm = new FrmQuanLyPHIEUYEUCAU();
            frm.ShowDialog();
        }

        private void ttbtnLapPhieuDatHang_Click(object sender, EventArgs e)
        {
            FrmLapPHIEUDATHANG frm = new FrmLapPHIEUDATHANG();
            frm.ShowDialog();
        }

        private void menThongTinLienHe_Click(object sender, EventArgs e)
        {
            FrmAboutMe frm = new FrmAboutMe();
            frm.ShowDialog();
        }

        private void ttbtnQuanLyPhieuYeuCau_Click(object sender, EventArgs e)
        {
            FrmQuanLyPHIEUYEUCAU frm = new FrmQuanLyPHIEUYEUCAU();
            frm.ShowDialog();
        }

        private void ttbtnQuanLyPhieuDatHang_Click(object sender, EventArgs e)
        {
            FrmQuanLyPHIEUDATHANG frm = new FrmQuanLyPHIEUDATHANG();
            frm.ShowDialog();
        }

        private void ttbtnQuanLyPhieuNhapKho_Click(object sender, EventArgs e)
        {
            FrmQuanLyPHIEUNHAPKHO frm = new FrmQuanLyPHIEUNHAPKHO();
            frm.ShowDialog();
        }

        private void ttbtnQuanLyPhieuXuatKho_Click(object sender, EventArgs e)
        {
            FrmQuanLyPHIEUXUATKHO frm = new FrmQuanLyPHIEUXUATKHO();
            frm.ShowDialog();
        }

        private void ttbtnLapPhieuYeuCau_Click(object sender, EventArgs e)
        {
            FrmLapPHIEUYEUCAU frm = new FrmLapPHIEUYEUCAU();
            frm.ShowDialog();
        }

        private void ttbtnTonKho_Click(object sender, EventArgs e)
        {
            FrmThongKeTONKHO frm = new FrmThongKeTONKHO();
            frm.ShowDialog();
        }
    }
}
